#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/indev/evdev.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>

#include "./../../inc/homePage.h"
#include "./../../inc/calculator.h"
#include "./../../inc/page_manager.h"
#include "./../../inc/login.h"
#include "./../../inc/album.h"
#include "./../../inc/music.h"

static lv_obj_t * volume_slider   = NULL;
static lv_obj_t * volume_popup    = NULL;
static lv_obj_t * home_volume_img = NULL;
static lv_obj_t * home_ac_img     = NULL;
static lv_obj_t * home_auto_img   = NULL;
static lv_obj_t * volume_img      = NULL;
static lv_obj_t * ac_img          = NULL;
static lv_obj_t * auto_img        = NULL;
lv_obj_t * home_open_close_img    = NULL;
lv_obj_t * open_close_img         = NULL;
static int ac_mode                = CLOSE_MODE;
static int auto_mode              = CLOSE_MODE;
static int volume_num             = 50;

// static lv_obj_t * home_Taskbar = NULL;

static void ac_btn_event_cb(lv_event_t * e);
static void auto_btn_event_cb(lv_event_t * e);

// 打开任务栏
void show_taskbar(lv_obj_t * parent)
{

    lv_obj_t * Taskbar = lv_obj_create(parent);
    lv_obj_set_size(Taskbar, 800, 60);
    lv_obj_set_pos(Taskbar, 0, 420);

    lv_obj_set_style_bg_color(Taskbar, lv_color_make(0, 0, 0), 0);
    lv_obj_set_style_border_width(Taskbar, 0, 0);
    lv_obj_set_style_radius(Taskbar, 0, 0);
    lv_obj_set_style_pad_all(Taskbar, 0, 0);
    // lv_obj_add_flag(bottom_bar,LV_OBJ_FLAG_HIDDEN);

    // 返回按钮
    lv_obj_t * back_btn = lv_btn_create(Taskbar);
    lv_obj_set_size(back_btn, LV_PCT(10), LV_PCT(100));
    lv_obj_align(back_btn, LV_ALIGN_LEFT_MID, LV_PCT(0), 0);
    lv_obj_set_style_bg_color(back_btn, lv_color_make(0, 0, 0), 0);
    lv_obj_set_style_radius(back_btn, 0, 0);
    lv_obj_add_event_cb(back_btn, back, LV_EVENT_CLICKED, NULL);
    // 添加按钮图标
    lv_obj_t * back_img = lv_img_create(back_btn);
    lv_img_set_src(back_img, "S:res/ui/back_w.png");
    lv_obj_center(back_img);

    // 返回主页
    lv_obj_t * home_btn = lv_btn_create(Taskbar);
    lv_obj_set_size(home_btn, LV_PCT(10), LV_PCT(100));
    lv_obj_set_style_bg_color(home_btn, lv_color_make(0, 0, 0), 0);
    lv_obj_set_style_radius(home_btn, 0, 0);
    lv_obj_align(home_btn, LV_ALIGN_LEFT_MID, LV_PCT(11), 0);
    lv_obj_add_event_cb(home_btn, back_to_home, LV_EVENT_CLICKED, NULL);
    // 添加按钮图标
    lv_obj_t * home_img = lv_img_create(home_btn);
    lv_img_set_src(home_img, "S:res/ui/home_w11.png");
    lv_obj_center(home_img);

    // apps
    lv_obj_t * apps_btn = lv_btn_create(Taskbar);
    lv_obj_set_size(apps_btn, LV_PCT(10), LV_PCT(100));
    lv_obj_set_style_bg_color(apps_btn, lv_color_make(0, 0, 0), 0);
    lv_obj_set_style_radius(apps_btn, 0, 0);
    lv_obj_align(apps_btn, LV_ALIGN_LEFT_MID, LV_PCT(22), 0);
    lv_obj_add_event_cb(apps_btn, open_apps, LV_EVENT_CLICKED, NULL);
    // 添加按钮图标
    lv_obj_t * apps_img = lv_img_create(apps_btn);
    lv_img_set_src(apps_img, "S:res/ui/menu_w.png");
    lv_obj_center(apps_img);

    // prev
    lv_obj_t * prev_btn = lv_btn_create(Taskbar);
    lv_obj_set_size(prev_btn, LV_PCT(10), LV_PCT(100));
    lv_obj_set_style_bg_color(prev_btn, lv_color_make(0, 0, 0), 0);
    lv_obj_set_style_radius(prev_btn, 0, 0);
    lv_obj_align(prev_btn, LV_ALIGN_LEFT_MID, LV_PCT(34), 0);
    lv_obj_add_event_cb(prev_btn, prev_event_cb, LV_EVENT_CLICKED, parent);
    // 添加按钮图标
    lv_obj_t * prev_img = lv_img_create(prev_btn);
    lv_img_set_src(prev_img, "S:res/ui/prev_w.png");
    lv_obj_center(prev_img);

    // open close music
    lv_obj_t * open_close_btn = lv_btn_create(Taskbar);
    lv_obj_set_size(open_close_btn, LV_PCT(10), LV_PCT(100));
    lv_obj_set_style_bg_color(open_close_btn, lv_color_make(0, 0, 0), 0);
    lv_obj_set_style_radius(open_close_btn, 0, 0);
    lv_obj_align(open_close_btn, LV_ALIGN_LEFT_MID, LV_PCT(45), 0);
    lv_obj_add_event_cb(open_close_btn, play_pause_event_cb, LV_EVENT_CLICKED, parent);
    // 添加按钮图标
    if(parent == home_page) {
        home_open_close_img = lv_img_create(open_close_btn);
        if(IS_PLAYING == CLOSE_MODE) {
            lv_img_set_src(home_open_close_img, "S:res/ui/open_music_w.png");
        } else {
            lv_img_set_src(home_open_close_img, "S:res/ui/close_music_w.png");
        }
        lv_obj_center(home_open_close_img);
    } else {
        open_close_img = lv_img_create(open_close_btn);
        if(IS_PLAYING == CLOSE_MODE) {
            lv_img_set_src(open_close_img, "S:res/ui/open_music_w.png");
        } else {
            lv_img_set_src(open_close_img, "S:res/ui/close_music_w.png");
        }
        lv_obj_center(open_close_img);
    }

    // next
    lv_obj_t * next_btn = lv_btn_create(Taskbar);
    lv_obj_set_size(next_btn, LV_PCT(10), LV_PCT(100));
    lv_obj_set_style_bg_color(next_btn, lv_color_make(0, 0, 0), 0);
    lv_obj_set_style_radius(next_btn, 0, 0);
    lv_obj_align(next_btn, LV_ALIGN_LEFT_MID, LV_PCT(56), 0);
    lv_obj_add_event_cb(next_btn, next_event_cb, LV_EVENT_CLICKED, parent);
    // 添加按钮图标
    lv_obj_t * next_img = lv_img_create(next_btn);
    lv_img_set_src(next_img, "S:res/ui/next_w.png");
    lv_obj_center(next_img);

    // auto
    lv_obj_t * auto_btn = lv_btn_create(Taskbar);
    lv_obj_set_size(auto_btn, LV_PCT(10), LV_PCT(100));
    lv_obj_set_style_bg_color(auto_btn, lv_color_make(0, 0, 0), 0);
    lv_obj_set_style_radius(auto_btn, 0, 0);
    lv_obj_align(auto_btn, LV_ALIGN_LEFT_MID, LV_PCT(67), 0);
    lv_obj_add_event_cb(auto_btn, auto_btn_event_cb, LV_EVENT_CLICKED, parent);
    // 添加按钮图标
    if(parent == home_page) {
        home_auto_img = lv_img_create(auto_btn);
        if(auto_mode == CLOSE_MODE) {
            lv_img_set_src(home_auto_img, "S:res/ui/auto_w.png");
        } else {
            lv_img_set_src(home_auto_img, "S:res/ui/auto_o.png");
        }
        lv_obj_center(home_auto_img);
    } else {
        auto_img = lv_img_create(auto_btn);
        if(auto_mode == CLOSE_MODE) {
            lv_img_set_src(auto_img, "S:res/ui/auto_w.png");
        } else {
            lv_img_set_src(auto_img, "S:res/ui/auto_o.png");
        }
        lv_obj_center(auto_img);
    }

    // 空调
    lv_obj_t * ac_btn = lv_btn_create(Taskbar);
    lv_obj_set_size(ac_btn, LV_PCT(10), LV_PCT(100));
    lv_obj_set_style_bg_color(ac_btn, lv_color_make(0, 0, 0), 0);
    lv_obj_set_style_radius(ac_btn, 0, 0);
    lv_obj_align(ac_btn, LV_ALIGN_LEFT_MID, LV_PCT(78), 0);
    lv_obj_add_event_cb(ac_btn, ac_btn_event_cb, LV_EVENT_CLICKED, parent);
    // 添加按钮图标
    if(parent == home_page) {
        home_ac_img = lv_img_create(ac_btn);
        if(ac_mode == CLOSE_MODE) {
            lv_img_set_src(home_ac_img, "S:res/ui/closekt.png");
        } else {
            lv_img_set_src(home_ac_img, "S:res/ui/openkt.png");
        }
        lv_obj_center(home_ac_img);
    } else {
        ac_img = lv_img_create(ac_btn);
        if(ac_mode == CLOSE_MODE) {
            lv_img_set_src(ac_img, "S:res/ui/closekt.png");
        } else {
            lv_img_set_src(ac_img, "S:res/ui/openkt.png");
        }
        lv_obj_center(ac_img);
    }

    // // 设置
    // lv_obj_t * setting_btn = lv_btn_create(Taskbar);
    // lv_obj_set_size(setting_btn, LV_PCT(10), LV_PCT(100));
    // lv_obj_set_style_bg_color(setting_btn, lv_color_make(0, 0, 0), 0);
    // lv_obj_set_style_radius(setting_btn, 0, 0);
    // lv_obj_align(setting_btn, LV_ALIGN_LEFT_MID, LV_PCT(80), 0);
    // lv_obj_add_event_cb(setting_btn, open_setting, LV_EVENT_CLICKED, NULL);
    // // 添加按钮图标
    // lv_obj_t * setting_img = lv_img_create(setting_btn);
    // lv_img_set_src(setting_img, "S:res/ui/setting_w.png");
    // lv_obj_center(setting_img);

    // // 声音
    lv_obj_t * volume_btn = lv_btn_create(Taskbar);
    lv_obj_set_size(volume_btn, LV_PCT(10), LV_PCT(100));
    lv_obj_set_style_bg_color(volume_btn, lv_color_make(0, 0, 0), 0);
    lv_obj_set_style_radius(volume_btn, 0, 0);
    lv_obj_align(volume_btn, LV_ALIGN_LEFT_MID, LV_PCT(89), 0);
    lv_obj_add_event_cb(volume_btn, volume_btn_event_cb, LV_EVENT_CLICKED, parent);
    // 添加按钮图标
    if(parent == home_page) {
        home_volume_img = lv_img_create(volume_btn);
        if(volume_num == 0) {
            lv_img_set_src(home_volume_img, "S:res/ui/volume_xw.png");
        } else {
            lv_img_set_src(home_volume_img, "S:res/ui/volume_ow.png");
        }
        lv_obj_center(home_volume_img);
    } else {
        volume_img = lv_img_create(volume_btn);
        if(volume_num == 0) {
            lv_img_set_src(volume_img, "S:res/ui/volume_xw.png");
        } else {
            lv_img_set_src(volume_img, "S:res/ui/volume_ow.png");
        }
        lv_obj_center(volume_img);
    }
}

// 音量按钮点击事件
void volume_btn_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        // 如果滑动条已经显示，则隐藏
        if(volume_slider != NULL && lv_obj_is_valid(volume_slider)) {
            hide_volume_slider();
            return;
        }
        lv_obj_t * parent = lv_event_get_user_data(e);
        // 创建弹出窗口背景
        volume_popup = lv_obj_create(lv_scr_act());
        lv_obj_set_size(volume_popup, 60, 200);
        lv_obj_set_pos(volume_popup, 725, 120);
        lv_obj_set_style_bg_color(volume_popup, lv_color_hex(0x2C3E50), 0);
        lv_obj_set_style_bg_opa(volume_popup, LV_OPA_90, 0);
        lv_obj_set_style_radius(volume_popup, 10, 0);
        lv_obj_set_style_border_width(volume_popup, 2, 0);
        lv_obj_set_style_border_color(volume_popup, lv_color_hex(0x34495E), 0);
        lv_obj_clear_flag(volume_popup, LV_OBJ_FLAG_SCROLLABLE); // 不可滚动

        // 创建纵向滑动条
        volume_slider = lv_slider_create(volume_popup);
        lv_obj_set_size(volume_slider, 20, 150);
        lv_obj_align(volume_slider, LV_ALIGN_CENTER, 0, 0);
        lv_slider_set_range(volume_slider, 0, 100);
        lv_slider_set_value(volume_slider, volume_num, LV_ANIM_OFF); // 默认50%音量

        // 设置滑动条样式
        lv_obj_set_style_bg_color(volume_slider, lv_color_hex(0x34495E), LV_PART_MAIN);
        lv_obj_set_style_bg_color(volume_slider, lv_color_hex(0x3498DB), LV_PART_INDICATOR);
        lv_obj_set_style_bg_color(volume_slider, lv_color_hex(0xECF0F1), LV_PART_KNOB);

        // 添加滑动条事件
        lv_obj_add_event_cb(volume_slider, volume_slider_event_cb, LV_EVENT_VALUE_CHANGED, parent);
        lv_obj_clear_flag(volume_slider, LV_OBJ_FLAG_SCROLLABLE); // 不可滚动

        // 添加音量值标签
        lv_obj_t * value_label = lv_label_create(volume_popup);
        lv_label_set_text_fmt(value_label, "%d", lv_slider_get_value(volume_slider));
        lv_obj_align_to(value_label, volume_slider, LV_ALIGN_OUT_TOP_MID, 0, -10);
        lv_obj_set_style_text_color(value_label, lv_color_white(), 0);
        lv_obj_set_user_data(volume_slider, value_label);       // 将标签与滑动条关联
        lv_obj_clear_flag(value_label, LV_OBJ_FLAG_SCROLLABLE); // 不可滚动

        // 将弹出窗口移到最前面
        lv_obj_move_foreground(volume_popup);

        // 添加点击外部关闭的功能
        lv_obj_add_event_cb(lv_scr_act(), hide_volume_slider, LV_EVENT_CLICKED, NULL);
    }
}

// 滑动条值改变事件
void volume_slider_event_cb(lv_event_t * e)
{
    lv_obj_t * slider = lv_event_get_target(e);
    volume_num        = lv_slider_get_value(slider);

    // 更新音量值标签
    lv_obj_t * label = lv_obj_get_user_data(slider);
    if(label != NULL) {
        lv_label_set_text_fmt(label, "%d", volume_num);
    }
    lv_obj_t * parent = lv_event_get_user_data(e);
    if(ui_taskbar_color == 0) {
        if(volume_num == 0) {
            lv_img_set_src(home_volume_img, "S:res/ui/volume_xw.png");
        } else {
            lv_img_set_src(home_volume_img, "S:res/ui/volume_ow.png");
        }
    } else {
        if(volume_num == 0) {
            lv_img_set_src(home_volume_img, "S:res/ui/volume_xb.png");
        } else {
            lv_img_set_src(home_volume_img, "S:res/ui/volume_ob.png");
        }
    }
    if(parent != home_page) {
        if(ui_taskbar_color == 0) {
            if(volume_num == 0) {
                lv_img_set_src(volume_img, "S:res/ui/volume_xw.png");
            } else {
                lv_img_set_src(volume_img, "S:res/ui/volume_ow.png");
            }
        } else {
            if(volume_num == 0) {
                lv_img_set_src(volume_img, "S:res/ui/volume_xb.png");
            } else {
                lv_img_set_src(volume_img, "S:res/ui/volume_ob.png");
            }
        }
    }

    // 这里可以添加实际的音量控制代码
}

// 隐藏音量滑动条
void hide_volume_slider(void)
{
    if(volume_popup != NULL && lv_obj_is_valid(volume_popup)) {
        lv_obj_del(volume_popup);
        volume_popup  = NULL;
        volume_slider = NULL;
    }

    // 移除全局点击事件
    lv_obj_remove_event_cb(lv_scr_act(), hide_volume_slider);
}

static void ac_btn_event_cb(lv_event_t * e)
{
    lv_obj_t * parent = lv_event_get_user_data(e);
    if(ac_mode == CLOSE_MODE) {
        ac_mode = OPEN_MODE;
        lv_img_set_src(home_ac_img, "S:res/ui/openkt.png");
        if(parent != home_page) {
            lv_img_set_src(ac_img, "S:res/ui/openkt.png");
        }

    } else {
        ac_mode = CLOSE_MODE;
        lv_img_set_src(home_ac_img, "S:res/ui/closekt.png");
        if(parent != home_page) {
            lv_img_set_src(ac_img, "S:res/ui/closekt.png");
        }
    }
}

static void auto_btn_event_cb(lv_event_t * e)
{
    lv_obj_t * parent = lv_event_get_user_data(e);
    if(auto_mode == CLOSE_MODE) {
        auto_mode = OPEN_MODE;
        lv_img_set_src(home_auto_img, "S:res/ui/auto_o.png");
        if(parent != home_page) {
            lv_img_set_src(auto_img, "S:res/ui/auto_o.png");
        }
    } else {
        auto_mode = CLOSE_MODE;
        lv_img_set_src(home_auto_img, "S:res/ui/auto_w.png");
        if(parent != home_page) {
            lv_img_set_src(auto_img, "S:res/ui/auto_w.png");
        }
    }
}